package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysExamination;
import com.flyme.module.base.provider.mapper.SysExaminationMapper;
import com.flyme.module.base.provider.service.SysExatimeService;
import com.flyme.module.order.provider.service.OrderService;
import com.flyme.module.base.provider.service.SysExaminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysExaminationServiceImpl extends BaseServiceImpl<SysExaminationMapper, SysExamination> implements SysExaminationService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SysExatimeService exatimeService;

    @Override
    public SysExamination getByCode(String examinationCode){
        return getOne(q().eq("examinationCode", examinationCode), false);
    }

    @Override
    public Integer countByType(Long exanTypeId){
        return count(q().eq(true,"examTypeId", exanTypeId).eq(true, "isDel", 0));
    }

    @Override
    public Integer countByEnum(Long enumId){
        return count(q().eq(true,"enumId", enumId));
    }

    @Override
    public PqGrid selectIndex(PagerInfo pagerInfo){
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("id,examName,applyBeginDate,applyEndDate,examType");
        cq.eq("isDel", 0);
        cq.eq("isOpen", 1);
        cq.orderByDesc("createTime");
        PqGrid pqGrid = pageList(cq);
        List<EntityMap> list = (List<EntityMap>)pqGrid.getRows();
        for(EntityMap exam : list){
            Long id = exam.get("id");
            //查询订单金额及报名人数
            EntityMap order = orderService.getStatistics(id);
            exam.put("nop", order.get("nop"));
            exam.put("amount", order.get("amount"));
            Integer type = 2;//1未开始2进行中3已结束4没有
            EntityMap beginMap = exatimeService.getIndexBegin(id);
            EntityMap endMap = exatimeService.getIndexEnd(id);
            if(ObjectUtils.isEmpty(beginMap.get("beginTime")) || ObjectUtils.isEmpty(endMap.get("endTime"))){
                type = 4;
                exam.put("examStatusNow", type);
                continue;
            }
            String beginTime = beginMap.get("beginTime").toString();
            String endTime = endMap.get("endTime").toString();
            //小于0当前日期大于指定时间（当前日期大于最晚考试时间）
            if(DateUtil.between(DateUtil.date(), DateUtil.parseDateTime(endTime), DateUnit.MINUTE, false) < 0){
                type = 3;
            }
            //大于0当前日期小于指定时间（当前日期小于最早考试时间）
            if(DateUtil.between(DateUtil.date(), DateUtil.parseDateTime(beginTime), DateUnit.MINUTE, false) > 0){
                type = 1;
            }
            exam.put("examStatusNow", type);
        }
        pqGrid.setRows(list);
        return pqGrid;
    }


}
